<?php
/**
 * <OXmlWriter>
 *  Copyright (C) <2011>  <Omar Teodoro Alegre>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * ********************************************************
 * ********************************************************
 *                    OXmlWriter
 * ********************************************************
 * ********************************************************
 * 
 * @version 1.0
 * @created 28-ago-2011 07:07:53 p.m.
 * @author Omar Teodoro Alegre - alegreomarteodoro@yahoo.com.ar
 *  Corrientes, Argentina.
 */

require_once dirname(__FILE__) . '/OXmlElement.php';

class OXmlWriter
{

	protected $_body = NULL;
	protected $_root_element = NULL;
	protected $_params = array( 'version' 		=> '1.0',
								'encoding' 		=> 'ISO-8859-1',
								'standalone' 	=> ''
							   );
	protected $_comments = array();
	protected $_filename = 'xml_document';
	
	/** Constructor */
	
	public function __construct()
	{
		
	}

	/** Setters and Getters */
	
	/**
	 * Define el elemento Root del documento xml.
	 * @param OXmlElement root_element - Elemento.
	 */
	public function setRootElement( OXmlElement $root_element )
	{
		$this->_root_element = $root_element;
	}

	/**
	 * Devuelve el elemento Root del documento xml.
	 * @return OXmlElement - Elemento.
	 */
	public function getRootElement()
	{
		return $this->_root_element;
	}

	/**
	 * Define el Encoding del documento xml.
	 * @param String value - Por defecto ISO-8859-1.
	 */
	public function setEncoding( $value )
	{
		$this->_params['encoding'] = $value;
	}

	/**
	 * Define el Standalone del documento xml.
	 * @param String value
	 */
	public function setStandalone( $value )
	{
		$this->_params['standalone'] = $value;
	}

	/**
	 * Define la Version del documento xml.
	 * @param String value - Por defecto 1.0.
	 */
	public function setVersion( $value )
	{
		$this->_params['version'] = $value;
	}
	
	/**
	 * Devuelve un array con los parametros del prologo del documento xml.
	 * @return Array.
	 */
	protected function getParams()
	{
		return $this->_params;
	}
	
	/**
	 * Agrega comentarios al principio del documento (Luego del prologo).
	 * @param String $comment.
	 */
	public function addComment($comment)
	{
		$this->_comments[] = $comment;
	}
	
	/**
	 * Devuelve un array con los comentarios.
	 * @return Array.
	 */
	protected function getComments()
	{
		return $this->_comments;
	}
	
	/**
	 * Devuelve la Version del documento xml.
	 * @return String.
	 */
	public function getVersion()
	{
		return $this->_params['version'];
	}
	
	/**
	 * Devuelve el Standalone del documento xml.
	 * @return String.
	 */
	public function getStandalone()
	{
		return $this->_params['standalone'];
	}
	
	/**
	 * Devuelve el Encoding del documento xml.
	 * @return String.
	 */
	public function getEncoding()
	{
		return $this->_params['encoding'];
	}
	
	/**
	 * Define el nombre del archivo xml; 
	 * "Aclaracion: Dicho nombre solo es usa si se descarga el documento."
	 * @param String $filename.
	 */
	public function setFilename($filename)
	{
		$this->_filename = $filename;
	}
	
	/**
	 * Devuelve el nombre del archivo xml;
	 * "Aclaracion: Dicho nombre solo es usa si se descarga el documento."
	 * @return String.
	 */
	public function getFilename()
	{
		return $this->_filename;	
	}
	
	/**
	 * Descarga el documento xml.
	 */
	public function download( $filename = '' )
	{
		if( !empty($filename) )
			$this->setFilename($filename);
		
		header("Content-type: text/xml");
		header('Content-Disposition: attachment; filename="'.$this->getFilename().'.xml"');
		echo $this->toString();
	}
	
	/**
	 * Devuelve el prologo del documento xml.
	 * @return String.
	 */
	public function getPrologue()
	{
		$output = '<?xml';
		
		foreach ($this->getParams() as $key => $value ) 
		{
			if( $value != '' )
				$output .= " {$key}=\"{$value}\""; 
		}
			
		$output .= ' ?>';
		
		if( count($this->getComments()) > 0 )
		{
			foreach ($this->getComments() as $value ) 
			{
					$output .= "<!--{$value}-->"; 
			}
		}
		
		return $output;
	}
	
	/**
	 * Devuelve el cuerpo del documento xml.
	 * @return String.
	 */
	public function getBody()
	{
		if( !is_null( $this->getRootElement() ) )
			$this->_body .= $this->getRootElement()->toString();
		return $this->_body;
	}
	
	/** Metodos  */
	
	/**
	 * Muestra el documento xml.
	 */
	public function show()
	{
		header("Content-type: text/xml"); 
		echo $this->toString();
	}
	
	/**
	 * Elimina el elemento Root del documento xml.
	 */
	public function deleteRootElement()
	{
		unset($this->_root_element);
	}

	/**
	 * Devuelve una cadena con el contenido del documento xml.
	 * @return String. 
	 */
	public function toString()
	{				
		return $this->getPrologue() . $this->getBody();
	}
}
?>